import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/model/hand_over/hand_over_model.dart';
import 'package:ls_work/net/response/hand_over/hand_over_detail_info.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/widgets/text_field.dart';

import '../../a_yyt/models/handover_new_entity.dart';

class HandOverDetailViewModel extends BaseViewModel {
  HandoverNewInfo info;
  dynamic areaId;

  HandOverDetailInfo? detailInfo;

  HandOverDetailViewModel(super.context,
      {required this.info, required this.areaId});

  var model = HandOverModel();

  Future<void> requestDetail() async {
    showLoadingState();
    try {
      detailInfo = await model.handOverDetail(
          reportDate: info.reportDate
              ?.changeTime(format: ['yyyy', '-', 'mm', '-', 'dd']),
          dutyType: info.dutyType,
          areaId: areaId,
        startTime: info.startTime,
        endTime: info.endTime
      );
      showContent();
    } catch (e) {
      showErrorState(e.toNetError());
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    requestDetail();
  }

  var archiveStr = "";
  var archive = 0;

  void showHandOverArchiveBottomSheet() {
    archiveStr = detailInfo?.summary ?? "";
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(
                  height: 20,
                ),
                CommonText('交接班总结', textSize: 24, fontWeight: FontWeight.bold),
                SizedBox(
                  height: 20,
                ),
                NoBorderInput(
                        hintText: "请输入交接班总结",
                        textSize: 16,
                        maxLines: 6,
                        minLines: 6,
                        text: archiveStr,
                        onChanged: (str) {
                          archiveStr = str;
                        },
                        keyboardType: CommonInputType.multiline)
                    .addToContainer(
                        padding:
                            EdgeInsets.symmetric(horizontal: 12, vertical: 10),
                        decoration: BoxDecoration(
                            color: Colours.bg_color,
                            borderRadius: 6.borderRadius)),
                SizedBox(
                  height: 20,
                ),
                Row(
                  children: [
                    CheckWithText(
                      title: '归档(归档后，各业务人员将不能就追加信息，本交班报告归档，不允许进行其他修改)',
                      onChanged: (value) {
                        if (value) {
                          archive = 1;
                        } else {
                          archive = 0;
                        }
                        notifyListeners();
                      },
                      defaultSelected: archive == 1,
                    )
                  ],
                ),
                SizedBox(
                  height: 30,
                ),
                Row(
                  children: [
                    TextButton(
                      onPressed: () {
                        Go().pop();
                      },
                      child: CommonText('取消', textColor: Colors.grey),
                      style: ButtonStyle(
                          shape: WidgetStateProperty.all(StadiumBorder(
                              side: BorderSide(color: Colors.grey, width: 1)))),
                    ).setWeight(1),
                    SizedBox(
                      width: 20,
                    ),
                    TextButton(
                      onPressed: () {
                        submitArchive();
                      },
                      child: CommonText('发布', textColor: Colors.white),
                      style: ButtonStyle(
                          backgroundColor:
                              WidgetStateProperty.all(Colours.app_main),
                          shape: WidgetStateProperty.all(StadiumBorder(
                              side: BorderSide(
                                  color: Colours.app_main, width: 1)))),
                    ).setWeight(2)
                  ],
                ),
                SizedBox(
                  height: 20,
                )
              ],
            ),
          ).addToContainer(
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.vertical(top: 20.radius)),
              padding: EdgeInsets.only(
                  left: 16,
                  right: 16,
                  top: 20,
                  bottom: MediaQuery.of(context).viewInsets.bottom + 20));
        },
        backgroundColor: Colors.transparent,
        isScrollControlled: true,
        enableDrag: false);
  }

  Future<void> submitArchive() async {
    if (archiveStr.isEmpty) {
      showToast("请输入交接班总结");
      return;
    }

    showLoadingDialog(msg: '提交中...');
    try {
      await model.handOverArchive(
          id: detailInfo?.id, summary: archiveStr, archive: archive);
      hideDialog();
      Go().pop();
      archive = 0;
      archiveStr = "";
      requestDetail();
    } catch (e) {
      showToast(e.toNetError());
      hideDialog();
    }
  }
}
